package com.webflux.demo.webfluxdemo.controller;

import org.springframework.http.*;
import org.springframework.http.codec.multipart.FilePart;
import org.springframework.http.codec.multipart.Part;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.reactive.result.view.Rendering;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebSession;
import reactor.core.publisher.Mono;

import javax.swing.*;
import java.security.Principal;

/**
 * RequestContext
 */
@Controller
public class ArgumentController {

    @PostMapping("/upload")
    public String uploadFile(@RequestPart("meta-data") Part metadata,
                             @RequestPart("file-data") FilePart file) {
        return "";
    }

    /**
     * 新版页面跳转api，不能标注@ResponseBody
     *
     * @return
     */
    @GetMapping("/render")
    public Rendering render() {
//        Rendering.redirectTo("/response"); // 重定向到当前项目根路径下的aaa
        return Rendering.redirectTo("http://www.baidu.com").build();
    }

    /**
     * 快捷自定义响应
     *
     * @return
     */
    @GetMapping("/response")
    public ResponseEntity<String> response() {
        return ResponseEntity.status(305)
                .header("headerName", "headerValue")
                .contentType(MediaType.APPLICATION_CBOR)
                .body("abccc");
    }

    /**
     * @param serverWebExchange 封装了请求和响应对象
     * @param webSession        访问Session对象
     * @param httpMethod        请求方式
     *                          PathVariable路径变量
     *                          RequestParam请求参数
     *                          RequestHeader请求头
     *                          CookieValue 获取Cookie
     *                          RequestBody获取请求体，Post、文件上传
     *                          HttpEntity<B> 封装后的请求对象
     *                          RequestPart 获取文件上传的数据
     *                          <p>
     *                          https://docs.spring.io/spring-framework/reference/6.0/web/webflux/controller/ann-methods/arguments.html
     * @return
     */
    @ResponseBody
    @GetMapping("/exchange")
    public Mono<String> serverWebExchange(ServerWebExchange serverWebExchange,
                                          WebSession webSession,
                                          Principal principal,
                                          HttpMethod httpMethod,
                                          HttpEntity<String> entity,
                                          MultipartFile file,// 兼容以前的文件上传方式
                                          FilePart filePart) {
//        filePart.transferTo() // 零拷贝技术
        ServerHttpRequest request = serverWebExchange.getRequest();
        ServerHttpResponse response = serverWebExchange.getResponse();

        Object value = webSession.getAttribute("aaa");
        webSession.getAttributes().put("aaa", "bbb");

        HttpHeaders headers = entity.getHeaders();
        String body = entity.getBody();

        return Mono.just("abc");
    }
}
